package com.tanhua.sso.controller;

import com.tanhua.common.vo.ErrorResult;
import com.tanhua.sso.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author 王加鑫
 * @Date 2021/09/01 21:01
 * @Since JDK 1.8
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 完善用户个人信息资料
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity saveUserInfo(@RequestBody Map<String,String> map, @RequestHeader("Authorization") String token) {
        try {
            //调用userInfoService保存用户信息资料
            Boolean flag = this.userInfoService.saveUserInfo(map, token);
            if(flag){
                //新增用户个人信息资料成功
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("保存用户信息失败",e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存用户信息失败").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 上传用户个人头像
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto")MultipartFile file,@RequestHeader("Authorization")String token) {
        try {
            Boolean flag = this.userInfoService.saveUserLogo(file,token);
            if(flag){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("上传头像失败",e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("上传头像失败").build();
        return ResponseEntity.status(500).body(errorResult);
    }


}
